package spring.cloud.chapter2.controller;

//import io.github.yedaxia.apidocs.ApiDoc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import spring.cloud.chapter2.pojo.Account;
import spring.cloud.chapter2.vo.ResultMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试调用接口
 */
@Api(tags = "测试RestTemplate调用接口")
@RestController
public class CallRestTemplate {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * GET调用测试
     * @return
     */
//    @ApiDoc(result = String.class)
    @ApiOperation(value = "get测试",notes = "RestTemplate的GET测试")
    @GetMapping("/callget")
    public Map<String,Object> get(){
        String url = "http://localhost:8001/fund/account/{id}";
        Account account = restTemplate.getForObject(url,Account.class,1L);
        System.out.println(account.getAccountName());
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

    /**
     * POSt调用测试
     * @return
     */
//    @ApiDoc(result = String.class)
    @ApiOperation(value = "post测试",notes = "RestTemplate的POST测试")
    @GetMapping("/callpost")
    public Map<String,Object> post(){

        String url = "http://localhost:8001/fund/account";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));

        Account account = new Account();
        account.setAccountName("account_xxx");
        account.setBalance(12345.60);
        account.setNote("account_note_xxx");

        HttpEntity<Account> request = new HttpEntity<>(account,headers);
        Account result = restTemplate.postForObject(url,request,Account.class);
        System.out.println(result.getAccountName());

        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

    /**
     * PUT调用测试
     * @return
     */
//    @ApiDoc(result = String.class)
    @ApiOperation(value = "put测试",notes = "RestTemplate的PUT测试")
    @GetMapping("/callput")
    public Map<String,Object> put(){

        String url = "http://localhost:8001/fund/account";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));

        Account account = new Account();
        account.setAccountName("account_xxx");
        account.setBalance(12345.60);
        account.setNote("account_note_xxx");

        HttpEntity<Account> request = new HttpEntity<>(account,headers);
        restTemplate.put(url,request);

        System.out.println(account.getAccountName());
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

    /**
     * DEL调用测试
     * @return
     */
//    @ApiDoc(result = String.class)
    @ApiOperation(value = "del测试",notes = "RestTemplate的DELETE测试")
    @GetMapping("/calldel")
    public Map<String,Object> del(){

        String url = "http://localhost:8001/fund/account/{id}";

        restTemplate.delete(url,123L);

        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

    /**
     * POST调用测试2
     * @return
     */
//    @ApiDoc(result = String.class)
    @ApiOperation(value = "post2测试",notes = "RestTemplate的POST测试")
    @GetMapping("/callpost2")
    public Map<String,Object> post2(){

        String url = "http://localhost:8001/fund/account2";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));

        Account account = new Account();
        account.setAccountName("account_xxx");
        account.setBalance(12345.60);
        account.setNote("account_note_xxx");

        HttpEntity<Account> request = new HttpEntity<>(account,headers);
        ResponseEntity<Account> result = restTemplate.postForEntity(url,request,Account.class);
        HttpStatus status = result.getStatusCode();
        String success = result.getHeaders().get("success").get(0);
        if("true".equals(success)){
            Account accountResult = result.getBody();
            System.out.println(accountResult.getId());
        }else {
            String message = result.getHeaders().get("message").get(0);
            System.out.println(message);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }
}
